package com.ruoyi.common.sms.ali;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.sms.MsgResult;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;


/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/5/29 16:28
 * @Modified By:
 */
@Slf4j
public class AliMsgApi  {

    private static String SIGN_NAME = "代驾审车";
    private static String ACCESSKEYID = "LTAI4G6yD5Fzkt5W5TAqm1Jh";
    private static String SECRET = "okguXJ6nnEGVE6eNsWnhbnWMDpCclY";
    private static String REGIONID = "default";
    private static String OK = "OK";

    /**
     * 发送短信(验证码，通知)
     *
     */
    public static MsgResult sendMsg(String mobile, MsgBizEnum msgBizEnum, Map<String,String > params) {

        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers",mobile);
        request.putQueryParameter("SignName",SIGN_NAME);
        request.putQueryParameter("TemplateCode",msgBizEnum.getCode());
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(params));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject object = (JSONObject) JSONObject.parse(response.getData());
            String code = object.getString("Code");
            String message = object.getString("Message");
            if (OK .equals(code)){
                return MsgResult.success(code,message);
            }else{
                return MsgResult.fail(code,message);
            }
        } catch (Exception e) {
            log.warn("阿里云短信发送异常:{}",mobile,e);
        }

        return MsgResult.fail();
    }


    /**
     * 发送营销短息
     *
     * @param mobile
     * @param msgBizEnum
     * @return
     */
    public static MsgResult sendMsg(String mobile, MsgBizEnum msgBizEnum) {

        DefaultProfile profile = DefaultProfile.getProfile(REGIONID, ACCESSKEYID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers",mobile);
        request.putQueryParameter("SignName",SIGN_NAME);
        request.putQueryParameter("TemplateCode",msgBizEnum.getCode());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject object = (JSONObject) JSONObject.parse(response.getData());
            String code = object.getString("Code");
            String message = object.getString("Message");
            if (OK .equals(code)){
                return MsgResult.success(code,message);
            }else{
                return MsgResult.fail(code,message);
            }
        } catch (Exception e) {
            log.warn("阿里云短信发送异常:{}",mobile,e);
        }

        return MsgResult.fail();
    }


    public static void main(String[] args) {
        sendMsg("18267135626",MsgBizEnum.DJSC_YHQ);

//        Map<String,String> param = new HashMap<>();
//        param.put("code","1234");
//        sendMsg("18736000723",MsgBizEnum.LOGIN,param);
    }
}
